<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\CarOwner;
use App\Services\QrCodeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CarOwnerController extends Controller
{
    /**
     * 保存车主手机号
     */
    public function savePhoneNumber(Request $request)
    {
        $request->validate([
            'phone_number' => 'required|regex:/^1[3-9]\d{9}$/'
        ]);
        
        $user = auth()->user();
        $phoneNumber = $request->input('phone_number');
        
        Log::info('开始保存车主手机号', [
            'user_id' => $user->id,
            'phone_number' => $phoneNumber
        ]);
        
        try {
            // 查找或创建车主信息
            $carOwner = CarOwner::updateOrCreate(
                ['user_id' => $user->id],
                ['phone_number' => $phoneNumber]
            );
            
            Log::info('车主信息已保存', [
                'car_owner_id' => $carOwner->id,
                'phone_number' => $carOwner->phone_number
            ]);
            
            // 生成二维码
            Log::info('开始生成二维码');
            $this->generateQrCode($carOwner);
            Log::info('二维码生成完成');
            
            return response()->json([
                'status' => 'success',
                'message' => '手机号码已保存',
                'car_owner_id' => $carOwner->id
            ]);
        } catch (\Exception $e) {
            Log::error('保存手机号失败', ['error' => $e->getMessage()]);
            return response()->json([
                'status' => 'error',
                'message' => '保存手机号失败: ' . $e->getMessage()
            ], 500);
        }
    }
    
    /**
     * 获取车主二维码
     */
    public function getQrCode()
    {
        $user = auth()->user();
        $carOwner = CarOwner::where('user_id', $user->id)->first();
        
        if (!$carOwner || !$carOwner->phone_number) {
            return response()->json([
                'status' => 'error',
                'message' => '请先设置手机号码'
            ], 400);
        }
        
        if (!$carOwner->qr_code_path) {
            $this->generateQrCode($carOwner);
            $carOwner->refresh();
        }
        
        return response()->json([
            'status' => 'success',
            'qr_code_url' => url($carOwner->qr_code_path),
            'phone_number' => $carOwner->phone_number
        ]);
    }
    
    /**
     * 生成二维码
     * 
     * @param CarOwner $carOwner
     */
    protected function generateQrCode(CarOwner $carOwner)
    {
        Log::info('开始生成二维码', [
            'car_owner_id' => $carOwner->id,
            'user_id' => $carOwner->user_id
        ]);
        
        // 生成唯一标识符
        $identifier = encrypt([
            'id' => $carOwner->id,
            'timestamp' => time()
        ]);
        
        Log::info('生成的加密标识符', [
            'identifier_length' => strlen($identifier),
            'identifier_preview' => substr($identifier, 0, 50) . '...'
        ]);
        
        // 存储二维码数据
        $carOwner->qr_code_data = $identifier;
        
        // 生成二维码图片
        $qrCodeService = app(QrCodeService::class);
        $qrCodePath = $qrCodeService->generate($identifier);
        
        Log::info('二维码生成完成', [
            'qr_code_path' => $qrCodePath,
            'qr_code_data' => $identifier
        ]);
        
        // 更新数据库
        $carOwner->qr_code_path = $qrCodePath;
        $carOwner->save();
        
        Log::info('数据库更新完成', [
            'car_owner_id' => $carOwner->id,
            'qr_code_data_saved' => !empty($carOwner->qr_code_data),
            'qr_code_path_saved' => !empty($carOwner->qr_code_path)
        ]);
    }
}









    